﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class UIFieldRunMapCombatLog : MonoBehaviour
    {
        public UIFieldRunMap page;
        public GameObject template;
        public UIGrid grid;
        List<GameObject> gos = new List<GameObject>();

        void Awake()
        {
            template.SetActive(false);
        }

        public void Exit()
        {
            foreach(var obj in gos)
            {
                GameObject.Destroy(obj);
            }
            gos.Clear();
            gameObject.SetActive(false);
        }

        public void Init()
        {
            CSSideGetPvpMVList list = new CSSideGetPvpMVList();
            NetAPI.SendHttp(OpDefine.CSSideGetPvpMVList, list, (obj, ok) => 
            {
                if(ok)
                {
                    gameObject.SetActive(true);
                    InitView((SCSideGetPvpMVList)obj.kBody);
                    page.battleRedTip.gameObject.SetActive(false);
                }
            });
        }


        void InitView(SCSideGetPvpMVList list)
        {
            StartCoroutine(_InitView(list));
        }
    
        IEnumerator _InitView(SCSideGetPvpMVList list)
        {
            foreach (var obj in gos)
            {
                GameObject.Destroy(obj);
            }
            gos.Clear();
            grid.Reposition();
            yield return null;

            int i = 0;
            foreach (var mv in list.mvs)
            {
                var inst = GameObject.Instantiate(template);
                inst.transform.SetParent(grid.transform);
                inst.transform.localScale = Vector3.one;
                inst.SetActive(true);
                inst.GetComponent<UIFieldRunMapCombatLogTemplate>().Init(mv);
                gos.Add(inst);
                if (++i % 4 == 0)
                    yield return null;
                grid.Reposition();
                
            }
        }
        
    }
}